# 配置统一性验证脚本
Write-Host "=== 存储配置统一性检查 ===" -ForegroundColor Green
Write-Host ""

# 设置项目路径
$projectPath = "d:\2025\JGSY.PP\Project\JGSY.CMS.LowCode.Platform"
Set-Location $projectPath

# 检查配置文件
$configFiles = @(
    "appsettings.json",
    "appsettings.Development.json", 
    "appsettings.Production.json"
)

Write-Host "1. 检查配置文件状态:" -ForegroundColor Yellow
foreach ($file in $configFiles) {
    if (Test-Path $file) {
        Write-Host "✓ $file 存在" -ForegroundColor Green
    } else {
        Write-Host "✗ $file 不存在" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "2. 编译项目:" -ForegroundColor Yellow
dotnet build --configuration Release --verbosity quiet
if ($LASTEXITCODE -eq 0) {
    Write-Host "✓ 项目编译成功" -ForegroundColor Green
} else {
    Write-Host "✗ 项目编译失败" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "3. 配置内容检查:" -ForegroundColor Yellow

# 检查主配置文件
if (Test-Path "appsettings.json") {
    $config = Get-Content "appsettings.json" | ConvertFrom-Json -ErrorAction SilentlyContinue
    if ($config) {
        Write-Host "✓ appsettings.json 格式正确" -ForegroundColor Green
        
        # 检查Upload配置
        if ($config.Upload) {
            Write-Host "  - Upload配置存在" -ForegroundColor Cyan
            Write-Host "    StorageType: $($config.Upload.StorageType)" -ForegroundColor White
            if ($config.Upload.Local) {
                Write-Host "    Local.RootPath: $($config.Upload.Local.RootPath)" -ForegroundColor White
                Write-Host "    Local.BaseUrl: $($config.Upload.Local.BaseUrl)" -ForegroundColor White
            }
        } else {
            Write-Host "  - Upload配置缺失" -ForegroundColor Red
        }
        
        # 检查CloudStorage配置
        if ($config.CloudStorage) {
            Write-Host "  - CloudStorage配置存在" -ForegroundColor Cyan
            Write-Host "    StorageType: $($config.CloudStorage.StorageType)" -ForegroundColor White
            if ($config.CloudStorage.Local) {
                Write-Host "    Local.RootPath: $($config.CloudStorage.Local.RootPath)" -ForegroundColor White
                Write-Host "    Local.BaseUrl: $($config.CloudStorage.Local.BaseUrl)" -ForegroundColor White
            }
        } else {
            Write-Host "  - CloudStorage配置缺失" -ForegroundColor Red
        }
        
        # 一致性检查
        if ($config.Upload -and $config.CloudStorage -and $config.Upload.Local -and $config.CloudStorage.Local) {
            Write-Host ""
            Write-Host "一致性检查:" -ForegroundColor Yellow
            
            if ($config.Upload.Local.RootPath -eq $config.CloudStorage.Local.RootPath) {
                Write-Host "✓ RootPath一致" -ForegroundColor Green
            } else {
                Write-Host "✗ RootPath不一致: Upload($($config.Upload.Local.RootPath)) vs CloudStorage($($config.CloudStorage.Local.RootPath))" -ForegroundColor Red
            }
            
            if ($config.Upload.Local.BaseUrl -eq $config.CloudStorage.Local.BaseUrl) {
                Write-Host "✓ BaseUrl一致" -ForegroundColor Green
            } else {
                Write-Host "✗ BaseUrl不一致: Upload($($config.Upload.Local.BaseUrl)) vs CloudStorage($($config.CloudStorage.Local.BaseUrl))" -ForegroundColor Red
            }
        }
    } else {
        Write-Host "✗ appsettings.json 格式错误" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "4. API端点测试建议:" -ForegroundColor Yellow
Write-Host "启动应用后，可以使用以下API端点进行配置管理:" -ForegroundColor Cyan
Write-Host "GET  /api/StorageConfiguration/validate              - 验证配置一致性" -ForegroundColor White
Write-Host "GET  /api/StorageConfiguration/unified               - 获取统一配置" -ForegroundColor White
Write-Host "GET  /api/StorageConfiguration/recommendations       - 获取修复建议" -ForegroundColor White
Write-Host "POST /api/StorageConfiguration/auto-fix              - 自动修复当前环境" -ForegroundColor White
Write-Host "POST /api/StorageConfiguration/fix-all-environments  - 修复所有环境" -ForegroundColor White

Write-Host ""
Write-Host "5. 配置统一性工具已就绪:" -ForegroundColor Yellow
Write-Host "- StorageConfigurationUnifier类：配置统一管理" -ForegroundColor Cyan
Write-Host "- ConfigurationDifferenceAnalyzer类：配置差异分析" -ForegroundColor Cyan
Write-Host "- StorageConfigurationController：配置管理API" -ForegroundColor Cyan

Write-Host ""
Write-Host "是否启动应用进行测试? (Y/N): " -NoNewline -ForegroundColor Yellow
$response = Read-Host
if ($response -eq "Y" -or $response -eq "y") {
    Write-Host "正在启动应用..." -ForegroundColor Green
    Write-Host "访问 https://localhost:7001/swagger 查看API文档" -ForegroundColor Cyan
    Write-Host "按 Ctrl+C 停止应用" -ForegroundColor Yellow
    dotnet run --configuration Release
} else {
    Write-Host "测试完成。" -ForegroundColor Green
}
